<?php
namespace Admin\Controller;

class DiaryController extends BaseController {
    public function __construct()
    {
        $this->setWebTitle('日记')
            ->setIdentifier('article');
        //     ->setLicense(false)
        //     ->addCrumb(L('index'), U('Home/Index/index'));

        parent::__construct();
    }

    // 首页
    public function indexAction()
    {
        $this->assign('identifier', 'article.diary');
       
        $this->display('index');
    }

    public function pageAction()
    {
        if(IS_GET){
            $current_page = I('get.page/d');
            $num = I('get.limit/d');;

            //加载分页
            $total_page = M('Diary')->count();
            $results = D('Diary')->pages($current_page, $num, array('id'=>'asc'));

            $data = array(
                'code'  => 0,
                'msg'   => '',
                'count' => $total_page,
                'data'  => $results
            );
            $this->ajaxReturn($data, 'json');
        }
        return $this->error404Page();
    }

    public function addDiaryAction()
    {
        if(IS_POST){

            $content = I('post.content', '');

            $error = '';
            $success = D('Diary')->add($content, $error);

            if ($success) {
                $this->successAjax('日记添加成功');
            } else {
                $this->errorAjax($error);
            }    
        }
    }

    public function editDiaryAction()
    {
        if(IS_POST){
            $data = I('post.data', '');

            $error = '';
            $success = D('Diary')->update($data, $error);

            if ($success) {
                $this->successAjax('日记更新成功');
            } else {
                $this->errorAjax($error);
            }    
        }
    }

    public function deleteDiaryAction()
    {
        if(IS_POST){

            $id = I('post.id', '');

            $error = '';
            if(is_array($id)){
                foreach ($id as $i) {
                    $success = D('Diary')->delete($i, $error);
                }
            }else{
                $success = D('Diary')->delete($id, $error);
            }

            if ($success) {
                $this->successAjax('删除日记成功');
            } else {
                $this->errorAjax($error);
            }    
        }
    }

    public function findOneAction()
    {
        if(IS_POST){
            $id = I('post.id', '');

            $error = '';
            $data = D('Diary')->findOne($id, $error);

            if ($data) {
                $this->successAjax($data);
            } else {
                $this->errorAjax($error);
            }    
        }
    }
}